/**
 * 
 */
Ext.define("appfe.ux.UXStatusBar", {
    extend: "Ext.toolbar.Toolbar",
    //alternateClassName: "Ext.ux.StatusBar",
    alias: "widget.uxstatusbar",
    requires: ["Ext.toolbar.TextItem"],
    cls: "x-statusbar",
    busyIconCls: "x-status-busy",
    busyText: "Loading...",
    autoClear: 5000,
    emptyText: "",
    activeThreadId: 0,
    initComponent: function() {
        if (this.statusAlign === "right") {
            this.cls += " x-status-right"
        }
        this.callParent(arguments)
    },
    afterRender: function() {
        Ext.suspendLayouts();
        this.callParent(arguments);
        var right = this.statusAlign === "right";
        this.currIconCls = this.iconCls || this.defaultIconCls;
        this.statusEl = Ext.create("Ext.toolbar.TextItem", {
            cls: "x-status-text " + (this.currIconCls || ""),
            text: this.text || this.defaultText || ""
        });
        if (right) {
            this.add("->");
            this.add(this.statusEl)
        } else {
            this.insert(0, this.statusEl);
            this.insert(1, "->")
        }
        //this.height = 27;
        Ext.resumeLayouts(true);
    },
    setStatus: function(d) {
        d = d || {};
        Ext.suspendLayouts();
        if (Ext.isString(d)) {
            d = { text: d }
        }
        if (d.text !== undefined) {
            this.setText(d.text)
        }
        if (d.iconCls !== undefined) {
            this.setIcon(d.iconCls)
        }
        if (d.clear) {
            var e = d.clear,
                wait = this.autoClear,
                defaults = {
                    useDefaults: false, //Modified By WangHaiTao 不使用默认的配置，因为没有配置默认配置
                    anim: false //不使用动态效果，直接消失
                };
            if (Ext.isObject(e)) {
                e = Ext.applyIf(e, defaults);
                if (e.wait) {
                    wait = e.wait
                }
            } else {
                if (Ext.isNumber(e)) {
                    wait = e;
                    e = defaults;
                } else {
                    if (Ext.isBoolean(e)) {
                        e = defaults;
                    }
                }
            }
            e.threadId = this.activeThreadId;
            Ext.defer(this.clearStatus, wait, this, [e])
        }
        Ext.resumeLayouts(true);
        return this
    },
    /**
     * [clearStatus description]
     * @param  {[type]} c [description]
     * @return {[type]}   [description]
     */
    clearStatus: function(c) {
        //console.debug('clearStatus');       
        c = c || {};
        if (this.destroyed || c.threadId && c.threadId !== this.activeThreadId) {
            // this means the current call was made internally, but a newer 
            // thread has set a message since this call was deferred.  Since 
            // we don't want to overwrite a newer message just ignore.             
            return this;
        }
        var text = c.useDefaults ? this.defaultText : this.emptyText,
            iconCls = c.useDefaults ? (this.defaultIconCls ? this.defaultIconCls : "") : "";
        if (c.anim) {
            this.statusEl.el.puff({
                remove: false,
                useDisplay: true,
                callback: function() {
                    this.statusEl.el.show();
                    this.setStatus({
                        text: text,
                        iconCls: iconCls
                    });
                }
            })
        } else {
            this.setStatus({
                text: text,
                iconCls: iconCls
            });
        }
        return this;
    },
    setText: function(a) {
        this.activeThreadId++;
        this.text = a || "";
        if (this.rendered) {
            this.statusEl.setText(this.text);
        }
        return this;
    },
    getText: function() {
        return this.text;
    },
    setIcon: function(a) {
        this.activeThreadId++;
        a = a || "";
        if (this.rendered) {
            if (this.currIconCls) {
                this.statusEl.removeCls(this.currIconCls);
                this.currIconCls = null
            }
            if (a.length > 0) {
                this.statusEl.addCls(a);
                this.currIconCls = a
            }
        } else {
            this.currIconCls = a
        }
        return this;
    },
    showBusy: function(a) {
        if (Ext.isString(a)) {
            a = { text: a };
        }
        a = Ext.applyIf(a || {}, {
            text: this.busyText,
            iconCls: this.busyIconCls
        });
        return this.setStatus(a)
    }
});